package es.xpt.ysf.commons.report;

import java.io.InputStream;
import java.util.Hashtable;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import es.xpt.ysf.commons.report.model.Report;
import es.xpt.ysf.commons.report.model.Reports;

public class ReportXML { // NO_UCD
	private Hashtable<String, Report> reportsHash;

	public ReportXML(Hashtable<String, Report> reportsHash) {
		this.reportsHash = reportsHash;
	}

	public Report getReport(String typeReport) throws ReportException {
		return reportsHash.get(typeReport);
	}

	public Hashtable<String, Report> getReports() throws ReportException {
		return reportsHash;
	}

	public static void load(String url, Hashtable<String, Report> reportsHash) throws ReportException {
		try {
			InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + url);
			
				JAXBContext jaxbContextResponse = JAXBContext.newInstance("es.xpt.ysf.commons.report.model");
				Unmarshaller marshaller = jaxbContextResponse.createUnmarshaller();
				Reports r = (Reports) marshaller.unmarshal(is);
				
			for (Report rbXML : r.getReport()) {
				reportsHash.put(rbXML.getId(), rbXML);
			}
		} catch (JAXBException e) {
			throw new ReportException(e);
		}

	}

}
